package com.lv.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.lv.entity.DingLink;
import com.lv.xulin.demopro.R;

import java.util.List;
import java.util.Map;

public class EvAdapter extends BaseExpandableListAdapter {

    private Map<String, List<String>> dataset ;
    private List<DingLink> parentList ;
    private Context context;

    public EvAdapter(Context context, Map<String, List<String>> dataset,List<DingLink>  parentList){
        this.context=context;
        this.dataset=dataset;
        this.parentList=parentList;
    }

    //  获得某个父项的某个子项
    @Override
    public Object getChild(int parentPos, int childPos) {
        return dataset.get(parentList.get(parentPos).getFname()).get(childPos);
    }

    //  获得父项的数量
    @Override
    public int getGroupCount() {
        return dataset.size();
    }

    //  获得某个父项的子项数目
    @Override
    public int getChildrenCount(int parentPos) {
        return dataset.get(parentList.get(parentPos)).size();
    }

    //  获得某个父项
    @Override
    public Object getGroup(int parentPos) {
        return dataset.get(parentList.get(parentPos).getFname());
    }

    //  获得某个父项的id
    @Override
    public long getGroupId(int parentPos) {
        return parentPos;
    }

    //  获得某个父项的某个子项的id
    @Override
    public long getChildId(int parentPos, int childPos) {
        return childPos;
    }

    //  按函数的名字来理解应该是是否具有稳定的id，这个方法目前一直都是返回false，没有去改动过
    @Override
    public boolean hasStableIds() {
        return true;
    }

    //  获得父项显示的view
    @Override
    public View getGroupView(int parentPos, boolean b, View view, ViewGroup viewGroup) {
        if (view == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.item_ding_parent, null);
        }
        view.setTag(R.layout.item_ding_parent, parentPos);
        view.setTag(R.layout.item_ding_child, -1);
        TextView text = (TextView) view.findViewById(R.id.parent_title);
        text.setText(parentList.get(parentPos).getFname());
        return view;
    }

    //  获得子项显示的view
    @Override
    public View getChildView(int parentPos, int childPos, boolean b, View view, ViewGroup viewGroup) {
            if (view == null) {
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = inflater.inflate(R.layout.item_ding_child, null);
            }
            view.setTag(R.layout.item_ding_parent, parentPos);
            view.setTag(R.layout.item_ding_child, childPos);
//            if(!parentList.get(parentPos).isExp()){
//                return view;
//            }
            TextView text = (TextView) view.findViewById(R.id.child_title);
            text.setText(dataset.get(parentList.get(parentPos).getFname()).get(childPos));
            text.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(context, "点到了内置的textview", Toast.LENGTH_SHORT).show();
                }
            });
            return view;
        }

    //  子项是否可选中，如果需要设置子项的点击事件，需要返回true
    @Override
    public boolean isChildSelectable(int i, int i1) {
        return false;
    }
}